package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyImgBean;
import flc.ast.bean.MyImgItemBean;
import flc.ast.bean.MyPicAlbumBean;
import flc.ast.bean.MyPicLibraryBean;
import flc.ast.databinding.ActivityAddPicBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.MhzDateUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class AddPicActivity extends BaseAc<ActivityAddPicBinding> {

    private SelPictureAdapter selPictureAdapter;
    private List<String> listSel = new ArrayList<>();
    private boolean isAll = false;
    private int allNum = 0;//相册总素材


    @Override
    protected int onCreate() {
        return R.layout.activity_add_pic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAddPicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        selPictureAdapter = new SelPictureAdapter();
        selPictureAdapter.setEdit(true);
        mDataBinding.rvPictureList.setAdapter(selPictureAdapter);
        selPictureAdapter.setOnItemClickListener(this);
        selPictureAdapter.setClickFeedback(new SelPictureAdapter.ClickFeedback() {
            @Override
            public void onClickFeedback(boolean isSel, String path) {
                if (isSel) {
                    listSel.add(path);
                } else {
                    listSel.remove(path);
                }
                if (listSel.size() == allNum) {
                    isAll = true;
                    mDataBinding.ivAllSel.setImageResource(R.drawable.ayiquanx);
                } else {
                    isAll = false;
                    mDataBinding.ivAllSel.setImageResource(R.drawable.aquanxuan);
                }
                mDataBinding.tvConfirm.setText(getString(R.string.add_num, listSel.size()));
            }
        });

        mDataBinding.llAllSel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llAllSel:
                isAll = !isAll;
                mDataBinding.ivAllSel.setImageResource(isAll ? R.drawable.ayiquanx : R.drawable.aquanxuan);
                selPictureAdapter.setAll(isAll);
                selPictureAdapter.notifyDataSetChanged();
                listSel.clear();
                for (MyImgBean bean : selPictureAdapter.getValidData()) {
                    if (isAll) {
                        for (MyImgItemBean myImgItemBean : bean.getList()) {
                            listSel.add(myImgItemBean.getPath());
                        }
                    }
                }
                mDataBinding.tvConfirm.setText(getString(R.string.add_num, listSel.size()));
                break;
            case R.id.tvConfirm://添加
                if (listSel.size() == 0) {
                    ToastUtils.showShort(R.string.please_sel_picture);
                    return;
                }
                saveToAlbum();
                break;
        }
    }

    @Override
    protected void initData() {
        getPermission();
        mDataBinding.tvConfirm.setText(getString(R.string.add_num, 0));
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_pic_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPictureList.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        showDialog(getString(R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<MyImgBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MyImgBean>> emitter) {
                List<String> list = new ArrayList<>();
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                for (SelectMediaEntity bean : listSys) {
                    list.add(bean.getPath());
                }
                List<MyImgBean> dayData = MhzDateUtils.getDayData(list);
                emitter.onNext(dayData);
            }

            @Override
            public void accept(List<MyImgBean> list) {
                dismissDialog();
                if (list != null && list.size() > 0) {
                    selPictureAdapter.setList(list);
                    allNum = list.size();
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                }
            }
        });
    }

    //保存选择文件到相册
    private void saveToAlbum() {
        showDialog(getString(R.string.add_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                List<MyPicLibraryBean> listSp = AppSpUtils.getPictureList();
                if (listSp != null && listSp.size() > 0) {
                    for (MyPicLibraryBean bean : listSp) {
                        if (bean.getId().equals(PicAlbumActivity.albumId)) {
                            if (bean.getList() != null && bean.getList().size() > 0) {
                                bean.getList().addAll(listSel);
                            } else {
                                bean.setList(listSel);
                            }
                            break;
                        }
                    }
                    AppSpUtils.savePictureList(listSp);
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                finish();
            }
        });
    }

}